package com.guchenghao.controller;

import com.guchenghao.dto.UserDto;
import com.guchenghao.entity.User;
import com.guchenghao.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @User 20675
 * @Author gch
 * @Date 2025/3/17 14:31
 * @Description <br />
 * <p>
 * <br />
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    // 跳转到 登录 页面
    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    // 跳转到 用户注册 页面
    @GetMapping("/register")
    public String toRegister() {
        return "register";
    }

    // 用户登录验证
    @PostMapping("/login")
    @ResponseBody
    public Boolean login(User user, HttpSession session) {
        return userService.login(user, session);
    }

    // 用户登录状态验证
    @GetMapping("/login/check")
    @ResponseBody
    public UserDto checkLogin(HttpSession session) {
        return userService.checkLogin(session);
    }

    // 用户注册
    @PostMapping("/register")
    @ResponseBody
    public Boolean register(@RequestBody User user) {
        return userService.register(user);
    }

    // 用户登出
    @PostMapping("/logout")
    @ResponseBody
    public String logout(@RequestParam String cookie) {
        return cookie;
    }

}
